package com.fanxuankai.boot.mqbroker.autoconfigure;

import com.fanxuankai.boot.mqbroker.ClearMsgTaskConfigurer;
import com.fanxuankai.boot.mqbroker.MqBrokerDingTalkClientHelper;
import com.fanxuankai.boot.mqbroker.consumer.MsgReceiveMapper;
import com.fanxuankai.boot.mqbroker.producer.MsgSendMapper;
import com.fanxuankai.commons.util.concurrent.ScheduledThreadPool;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author fanxuankai
 */
@EnableScheduling
@EnableTransactionManagement
@EnableConfigurationProperties(MqBrokerProperties.class)
@MapperScan(basePackageClasses = {MsgSendMapper.class, MsgReceiveMapper.class},
        annotationClass = Mapper.class)
public class MqBrokerAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor() {
        return ScheduledThreadPool.INSTANCE.getExecutor();
    }

    @Bean
    @ConditionalOnProperty(MqBrokerProperties.DING_TALK_ENABLED)
    public MqBrokerDingTalkClientHelper mqBrokerDingTalkClientHelper(MqBrokerProperties properties) {
        return new MqBrokerDingTalkClientHelper(properties);
    }

    @Bean
    public ClearMsgTaskConfigurer clearMsgTaskConfigurer() {
        return new ClearMsgTaskConfigurer();
    }
}
